class RotateBounce2 extends egret.DisplayObjectContainer {
    private ball: Ball
    private line: Line
    private gravity: number = 0.3
    private bounce: number = -0.6
    private touchX: number = 0
    private touchY: number = 0

    public constructor() {
        super()
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this)
    }

    private onAddToStage(event: egret.Event) {
        this.ball = new Ball()
        this.ball.x = 100
        this.ball.y = 100
        this.addChild(this.ball)

        this.line = new Line(0, 0, 300, 0)
        this.addChild(this.line)
        this.line.x = 50
        this.line.y = 100
        this.line.rotation = 0

        this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.trackTouch, this)
        this.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.trackTouch, this)
        this.stage.addEventListener(egret.TouchEvent.TOUCH_END, this.trackTouch, this)
        this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this)
    }

    private onEnterFrame(event: Event) {
        this.line.rotation = (this.stage.stageWidth / 2 - this.touchX) * .1
        // 标准的运动代码
        this.ball.vy += this.gravity
        this.ball.x += this.ball.vx
        this.ball.y += this.ball.vy

        // 回弹天花板、地板和墙
        if (this.ball.x + this.ball.radius > this.stage.stageWidth) {
            this.ball.x = this.stage.stageWidth - this.ball.radius
            this.ball.vx *= this.bounce
        } else if (this.ball.x - this.ball.radius < 0) {
            this.ball.x = this.ball.radius
            this.ball.vx *= this.bounce
        }

        if (this.ball.y + this.ball.radius > this.stage.stageHeight) {
            this.ball.y = this.stage.stageHeight - this.ball.radius
            this.ball.vy *= this.bounce
        } else if (this.ball.y - this.ball.radius < 0) {
            this.ball.y = this.ball.radius
            this.ball.vy *= this.bounce
        }

        if (this.hitTest(this.ball, this.line)) {
            // 获得角、正弦和余弦。
            let angle: number = this.line.rotation * Math.PI / 180
            let cos: number = Math.cos(angle)
            let sin: number = Math.sin(angle)

            // 获得小球相对于线条的位置。
            let x1: number = this.ball.x - this.line.x
            let y1: number = this.ball.y - this.line.y

            // 旋转坐标。
            let x2: number = cos * x1 + sin * y1
            let y2: number = cos * y1 - sin * x1

            // 使用旋转后的值执行回弹
            if (y2 > -this.ball.height / 2) {
                y2 = -this.ball.height / 2
                // 旋转速度。
                let vx1: number = cos * this.ball.vx + sin * this.ball.vy
                let vy1: number = cos * this.ball.vy - sin * this.ball.vx
                vy1 *= this.bounce

                // 将所有东西旋转回去
                x1 = cos * x2 - sin * y2
                y1 = cos * y2 + sin * x2
                this.ball.vx = cos * vx1 - sin * vy1
                this.ball.vy = cos * vy1 + sin * vx1
                this.ball.x = this.line.x + x1
                this.ball.y = this.line.y + y1
            }
        }
    }

    private rotate(cos: number, sin: number, ball: Ball) {
        let x1: number = ball.x - this.stage.stageWidth / 2
        let y1: number = ball.y - this.stage.stageHeight / 2
        let x2: number = cos * x1 - sin * y1
        let y2: number = cos * y1 + sin * x1
        ball.x = this.stage.stageWidth / 2 + x2
        ball.y = this.stage.stageHeight / 2 + y2
    }

    private hitTest(ball: egret.DisplayObject, line: egret.DisplayObject): boolean {
        var bounds: egret.Rectangle = line.getTransformedBounds(this)
        if (ball.x > bounds.left && ball.x < bounds.right) {
            return true
        }

        return false
    }

    private trackTouch(evt: egret.TouchEvent) {
        this.touchX = evt.stageX
        this.touchY = evt.stageY
    }
}